//
//  UIView+HUD.m
//  HTEducation
//
//  Created by htjy on 2017/4/28.
//  Copyright © 2017年 KELE. All rights reserved.
//

#import "UIView+HUD.h"
#import <MBProgressHUD/MBProgressHUD.h>

static const NSTimeInterval kDissmissDuration = 1.5f;

@implementation UIView (HUD)

- (MBProgressHUD *)current
{
    MBProgressHUD *hud = [MBProgressHUD HUDForView:self];
    if (!hud) {
        hud = [MBProgressHUD showHUDAddedTo:self animated:YES];
        hud.alpha = 0.85f;
        hud.bezelView.color = UIColor.blackColor;
        hud.contentColor = UIColor.whiteColor;
        hud.label.numberOfLines = 0;
        hud.label.font = [UIFont systemFontOfSize:14.f];
        hud.detailsLabel.numberOfLines = 0;
        hud.detailsLabel.font = [UIFont systemFontOfSize:12.f];
        hud.animationType = MBProgressHUDAnimationZoom;
        hud.removeFromSuperViewOnHide = YES;
    }
    hud.userInteractionEnabled = YES;
    return hud;
}

- (void)showLoading:(NSString *)text
{
    MBProgressHUD *hud = self.current;
    hud.mode = MBProgressHUDModeIndeterminate;
    hud.label.text = text;
}

- (void)showSuccess:(NSString *)text
{
    MBProgressHUD *hud = self.current;
    hud.mode = MBProgressHUDModeCustomView;
    hud.customView = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"HUD.bundle/success"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]] ;
    hud.label.text = text;
    [hud hideAnimated:YES afterDelay:kDissmissDuration];
}

- (void)showError:(NSString *)text
{
    MBProgressHUD *hud = self.current;
    hud.mode = MBProgressHUDModeCustomView;
    hud.customView = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"HUD.bundle/error"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]] ;
    hud.label.text = text;
    [hud hideAnimated:YES afterDelay:kDissmissDuration];
}

- (void)showInfo:(NSString *)text
{
    MBProgressHUD *hud = self.current;
    hud.mode = MBProgressHUDModeCustomView;
    hud.customView = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"HUD.bundle/info"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]] ;
    hud.label.text = text;
    [hud hideAnimated:YES afterDelay:kDissmissDuration];
}

- (void)showText:(NSString *)text
{
    MBProgressHUD *hud = self.current;
    hud.userInteractionEnabled = NO;
    hud.mode = MBProgressHUDModeText;
    hud.label.text = text;
    [hud hideAnimated:YES afterDelay:kDissmissDuration];
}

- (void)dismiss
{
    [MBProgressHUD hideHUDForView:self animated:YES];
}

@end
